package com.company.project.configurer;

import com.company.project.utils.LoggerUtils;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.io.IOException;
import java.util.Properties;

/**
 * IntelliJ IDEA.
 * User: lwb
 * Date: 2017/12/27
 * Time: 16:36
 * function:
 */
@Configuration
public class SchedulerConfig {

    @Bean(name="SchedulerFactory")
    public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        Properties quartzProperties = quartzProperties();
        if(quartzProperties.size() > 0)
            factory.setQuartzProperties(quartzProperties);
        return factory;
    }

    @Bean
    public Properties quartzProperties() throws IOException {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
//        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
        Properties properties = new Properties();
//        properties.put("org.quartz.threadPool.threadCount", "1");
        propertiesFactoryBean.setProperties(properties);
        //在quartz.properties中的属性被读取并注入后再初始化对象
        propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }

    /***quartz.properties
     * # 固定前缀org.quartz
     # 主要分为scheduler、threadPool、jobStore、plugin等部分
     #
     #
     org.quartz.scheduler.instanceName = DefaultQuartzScheduler
     org.quartz.scheduler.rmi.export = false
     org.quartz.scheduler.rmi.proxy = false
     org.quartz.scheduler.wrapJobExecutionInUserTransaction = false

     # 实例化ThreadPool时，使用的线程类为SimpleThreadPool
     org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool

     # threadCount和threadPriority将以setter的形式注入ThreadPool实例
     # 并发个数
     org.quartz.threadPool.threadCount = 5
     # 优先级
     org.quartz.threadPool.threadPriority = 5
     org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true

     org.quartz.jobStore.misfireThreshold = 5000

     # 默认存储在内存中
     #org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

     #持久化
     org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX

     org.quartz.jobStore.tablePrefix = QRTZ_

     org.quartz.jobStore.dataSource = qzDS

     org.quartz.dataSource.qzDS.driver = com.mysql.jdbc.Driver

     org.quartz.dataSource.qzDS.URL = jdbc:mysql://190.0.1.88:3306/hello_test?useUnicode=true&characterEncoding=UTF-8

     org.quartz.dataSource.qzDS.user = root

     org.quartz.dataSource.qzDS.password = root

     org.quartz.dataSource.qzDS.maxConnections = 10
     */
}
